﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using Kugar.Core.BaseStruct;
using Kugar.Core.Printer;
using Kugar.Core.WinForm.ExtMethod;

namespace Kugar.UI.WinForm.Print
{
    public partial class PrintManager : Office2007Form
    {
        private IPrint_ModulePrintProvider _provider = null;

        protected PrintManager()
        {
            InitializeComponent();


        }

        public PrintManager(IPrint_ModulePrintProvider provider)
            : this()
        {
            _provider = provider;
        }

        private void PrintManager_Load(object sender, EventArgs e)
        {
            lbFormatList.Items.Clear();

            lbFormatList.SelectedIndex = -1;

            loadFormatList();

            
        }


        private void lbFormatList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbFormatList.SelectedIndex >= 0)
            {

                btnEdit.Enabled = true;
                btnRefresh.Enabled = true;
                btnFormatDelete.Enabled = true;
                btnSetDefault.Enabled = true;
                btnExportToPDF.Enabled = true;
                btnExportToExcel.Enabled = true;
                btnPreview.Enabled = true;
                btnPrint.Enabled = true;
                btnExport.Enabled = true;
            }
            else
            {
                btnEdit.Enabled = false;
                btnRefresh.Enabled = false;
                btnFormatDelete.Enabled = false;
                btnSetDefault.Enabled = false;
                btnExportToPDF.Enabled = false;
                btnExportToExcel.Enabled = false;
                btnPreview.Enabled = false;
                btnPrint.Enabled = false;
                btnExport.Enabled = false;

            }
        }

        //编辑格式按钮事件
        private void btnFormatEdit_Click(object sender, EventArgs e)
        {
            if (lbFormatList.SelectedIndex < 0)
            {
                MessageBox.Show(@"请选择编辑的格式");
                return;
            }

            var printFormat = (ListItemObject<PrintFormatItemInfo>)lbFormatList.Items[lbFormatList.SelectedIndex];

            if (printFormat == null)
            {
                return;
            }

            try
            {
                var temp = (PrintFormatItemInfo)printFormat.Value.Clone();

                var isModify = _provider.Design(temp);

                if (isModify)
                {
                    printFormat.Value.PrintFormat = temp.PrintFormat;
                    _provider.Save(printFormat.Value.Name);
                }

                loadFormatList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //刷新读取格式按钮事件
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            lbFormatList.Items.Clear();
            loadFormatList();
        }

        //关闭窗口按钮
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //打印预览按钮事件
        private void btnPreview_Click(object sender, EventArgs e)
        {
            if (lbFormatList.SelectedIndex < 0)
            {
                MessageBox.Show(@"请选择编辑的格式");
                return;
            }

            var printFormat = (ListItemObject<PrintFormatItemInfo>)lbFormatList.Items[lbFormatList.SelectedIndex];

            if (printFormat == null)
            {
                return;
            }

            try
            {
                _provider.Preview(printFormat.Value.Name);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //直接打印按钮事件
        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (lbFormatList.SelectedIndex < 0)
            {
                MessageBox.Show(@"请选择编辑的格式");
                return;
            }

            var printFormat = (ListItemObject<PrintFormatItemInfo>)lbFormatList.Items[lbFormatList.SelectedIndex];

            if (printFormat == null)
            {
                return;
            }

            try
            {
                _provider.Print(printFormat.Value.Name);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //新增打印格式按钮事件
        private void btnAddNew_Click(object sender, EventArgs e)
        {
            try
            {
                if (_provider.CreateNew() != null)
                {
                    loadFormatList();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //删除打印格式按钮事件
        private void btnFormatDelete_Click(object sender, EventArgs e)
        {
            if (lbFormatList.SelectedIndex < 0)
            {
                MessageBox.Show(@"请选择编辑的格式");
                return;
            }

            var printFormat = (ListItemObject<PrintFormatItemInfo>)lbFormatList.Items[lbFormatList.SelectedIndex];

            if (printFormat == null)
            {
                return;
            }

            try
            {
                _provider.Delete(printFormat.Value.Name);

                loadFormatList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //导出为Excel按钮事件
        private void btnExportToExcel_Click(object sender, EventArgs e)
        {
            if (lbFormatList.SelectedIndex < 0)
            {
                MessageBox.Show(@"请选择用于导出PDF的打印格式");
                return;
            }

            var printFormat = (ListItemObject<PrintFormatItemInfo>)lbFormatList.Items[lbFormatList.SelectedIndex];

            if (printFormat == null)
            {
                return;
            }

            try
            {
                var filePath = DialogExt.ShowSaveFileDialog("Excel files (*.csv)|*.csv|All files (*.*)|*.*",
                                                            printFormat.Value.Name);

                if (!string.IsNullOrWhiteSpace(filePath))
                {
                    using (var stream = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        _provider.ExportToExcel(printFormat.Value.Name, stream);
                    }
                }

                //_provider.ExportToExcel(printFormat.Value.Name,);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //导出为PDF按钮事件
        private void btnExportToPDF_Click(object sender, EventArgs e)
        {
            if (lbFormatList.SelectedIndex < 0)
            {
                MessageBox.Show(@"请选择用于导出PDF的打印格式");
                return;
            }

            var printFormat = (ListItemObject<PrintFormatItemInfo>)lbFormatList.Items[lbFormatList.SelectedIndex];

            if (printFormat == null)
            {
                return;
            }

            try
            {
                var filePath = DialogExt.ShowSaveFileDialog("PDF files (*.pdf)|*.pdf|All files (*.*)|*.*",
                                                            printFormat.Value.Name);

                if (!string.IsNullOrWhiteSpace(filePath))
                {
                    using (var stream=File.Open(filePath,FileMode.OpenOrCreate,FileAccess.ReadWrite))
                    {
                        _provider.ExportToPDF(printFormat.Value.Name, stream);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void loadFormatList()
        {
            lbFormatList.Items.Clear();

            var itemList = _provider.GetModulePrintFormatList();

            if (itemList!=null && itemList.Count > 0)
            {
                for (int i = 0; i < itemList.Count; i++)
                {
                    string title = itemList[i].Name;

                    if (itemList[i].IsDefault)
                    {
                        title += "(默认)";
                    }

                    lbFormatList.Items.Add(new ListItemObject<PrintFormatItemInfo>(title, itemList[i]));

                    if (itemList[i].IsDefault)
                    {
                        lbFormatList.SelectedIndex = i;
                    }
                }                
            }
            else
            {
                lbFormatList.SelectedIndex = -1;
            }

            lbFormatList_SelectedIndexChanged(this, EventArgs.Empty);


        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            //btnExport.Popup(this.PointToClient(new Point(btnExport.Left, btnExport.Top + btnExport.Height)));
            btnExport.AutoExpandOnClick = true;
        }

        private void btnSetDefault_Click(object sender, EventArgs e)
        {
            if (lbFormatList.SelectedIndex < 0)
            {
                MessageBox.Show(@"请选择编辑的格式");
                return;
            }

            var printFormat = (ListItemObject<PrintFormatItemInfo>)lbFormatList.Items[lbFormatList.SelectedIndex];

            if (printFormat == null)
            {
                return;
            }

            try
            {
                _provider.SetDefault(printFormat.Value.Name);

                loadFormatList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
